<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-row :gutter="24">
        <el-col :md="6" :sm="12">
          <el-form-item label="授权人" prop="name">
            <el-input
              v-model="queryParams.uname"
              placeholder="请输入用户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="电话" prop="shopTel">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入联系人电话"
              clearable
              style="width: 140px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item style="margin-bottom: 20px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stores:branch:auth']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stores:branch:auth']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stores:branch:auth']"
        >导出
        </el-button>
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-table v-loading="loading" lazy :data="storeList" row-key="id"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="门店id" align="center" prop="id"/>
      <el-table-column label="授权人" align="center" prop="uname" width="200"/>
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" width="200">
        <template v-slot:default="scope">
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stores:branch:auth']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add-store-admin ref="addStore" @update="getList"/>
  </div>
</template>
<script>
import {getCityList} from "@/api/option";
import {branchAdminDel, getBranchAdmin} from "@/api/merchant/stores";
import addStoreAdmin from './computed/addStoreAdmin.vue'
import {listData} from "@/api/system/dict/data";

export default {
  name: "index",
  components: {addStoreAdmin},
  data() {
    return {
      ids: [],
      single: true,
      multiple: true,
      total: 0,
      loading: true,
      storeList: [],
      provinceOption: [],
      cityOption: [],
      areaOption: [],
      // 显示搜索
      showSearch: true,
      queryParams: {
        bid: '',
        uname: '',
        phone: '',
        pageNum: 1,
        pageSize: 10,
      }
    }
  },
  mounted() {
    this.queryParams.bid = this.$route.params && this.$route.params.bid;
    this.init()
  },
  methods: {
    init() {
      this.getList()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      let userIds = this.storeList.map(item => item.id)
      this.$refs.addStore.open(this.queryParams.bid)
    },
    handleDelete(row) {
      console.log(this.ids)
      const userIds = row.id || this.ids;
      console.log(userIds)
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
        branchAdminDel(userIds)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleExport() {
      this.download('/shop/branchadmin/export', {...this.queryParams}, `data_${new Date().getTime()}.xlsx`)
    },
    resetQuery() {
      let bid = this.queryParams.bid
      this.queryParams = {
        bid: bid,
        uname: '',
        phone: '',
        pageNum: 1,
        pageSize: 10,
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      getBranchAdmin(this.queryParams).then(res => {
        this.storeList = res.rows
        this.total = res.total
        this.loading = false
      })
    }
  },
}
</script>
<style lang="scss">
.el-table .warning-row {
  background: oldlace !important;
}
</style>
